package com.cbtx.utils

import android.app.ActivityManager
import android.content.Context
import android.os.Environment
import android.os.StatFs
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException
import java.text.DecimalFormat

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/7/1 12:50 PM
 * desc   :
 */
object FileSizeUtil {
    private val ERROR = -1

    /**
     *      * SDCARD是否存
     *     
     */
    fun externalMemoryAvailable(): Boolean {
        return Environment.getExternalStorageState() ==
                Environment.MEDIA_MOUNTED
    }

    /**
     *      * 获取手机内部剩余存储空间
     *      *
     *      * @return
     *     
     */
    fun getAvailableInternalMemorySize(): Long {
        val path = Environment.getDataDirectory()
        val stat = StatFs(path.path)
        val blockSize = stat.blockSize.toLong()
        val availableBlocks = stat.availableBlocks.toLong()
        return availableBlocks * blockSize
    }

    /**
     *      * 获取手机内部总的存储空间
     *      *
     *      * @return
     *     
     */
    fun getTotalInternalMemorySize(): Long {
        val path = Environment.getDataDirectory()
        val stat = StatFs(path.path)
        val blockSize = stat.blockSize.toLong()
        val totalBlocks = stat.blockCount.toLong()
        return totalBlocks * blockSize
    }

    /**
     *      * 获取SDCARD剩余存储空间
     *      *
     *      * @return
     *     
     */
    fun getAvailableExternalMemorySize(): Long {
        return if (externalMemoryAvailable()) {
            val path = Environment.getExternalStorageDirectory()
            val stat = StatFs(path.path)
            val blockSize = stat.blockSize.toLong()
            val availableBlocks = stat.availableBlocks.toLong()
            availableBlocks * blockSize
        } else {
            ERROR.toLong()
        }
    }

    /**
     *      * 获取SDCARD总的存储空间
     *      *
     *      * @return
     *     
     */
    fun getTotalExternalMemorySize(): Long {
        return if (externalMemoryAvailable()) {
            val path = Environment.getExternalStorageDirectory()
            val stat = StatFs(path.path)
            val blockSize = stat.blockSize.toLong()
            val totalBlocks = stat.blockCount.toLong()
            totalBlocks * blockSize
        } else {
            ERROR.toLong()
        }
    }

    /**
     *      * 获取系统总内存
     *      *
     *      * @param context 可传入应用程序上下文。
     *      * @return 总内存大单位为B。
     *     
     */
    fun getTotalMemorySize(context: Context?): Long {
        val dir = "/proc/meminfo"
        try {
            val fr = FileReader(dir)
            val br = BufferedReader(fr, 2048)
            val memoryLine = br.readLine()
            val subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"))
            br.close()
            return subMemoryLine.replace("\\D+".toRegex(), "").toInt() * 1024L
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return 0
    }

    /**
     *      * 获取当前可用内存，返回数据以字节为单位。
     *      *
     *      * @param context 可传入应用程序上下文。
     *      * @return 当前可用内存单位为B。
     *     
     */
    fun getAvailableMemory(context: Context): Long {
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val memoryInfo = ActivityManager.MemoryInfo()
        am.getMemoryInfo(memoryInfo)
        return memoryInfo.availMem
    }

    private val sFileIntegerFormat = DecimalFormat("#0")
    private val sFileDecimalFormat = DecimalFormat("#0.#")

    /**
     *      * 单位换算
     *      *
     *      * @param size 单位为B
     *      * @param isInteger 是否返回取整的单位
     *      * @return 转换后的单位
     *     
     */
    fun formatFileSize(size: Long, isInteger: Boolean): String? {
        val df = if (isInteger) sFileIntegerFormat else sFileDecimalFormat
        var fileSizeString = "0M"
        fileSizeString = if (size < 1024 && size > 0) {
            df.format(size.toDouble()) + "B"
        } else if (size < 1024 * 1024) {
            df.format(size.toDouble() / 1024) + "K"
        } else if (size < 1024 * 1024 * 1024) {
            df.format(size.toDouble() / (1024 * 1024)) + "M"
        } else {
            df.format(size.toDouble() / (1024 * 1024 * 1024)) + "G"
        }
        return fileSizeString
    }
}